package com.game.autoTool.project.web.common;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.mouse.*;

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GlobalMouseListener {
    public static void main(String[] args) {

        // 获取 JNativeHook 的日志记录器
        Logger logger = Logger.getLogger("org.jnativehook");

        // 创建一个 ConsoleHandler，并设置为禁用级别
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setLevel(Level.OFF);

        // 将日志处理器添加到 JNativeHook 的日志记录器
        logger.addHandler(consoleHandler);

        try {
            // 关闭JNativeHook的调试日志输出
            Logger.getLogger(GlobalScreen.class.getPackage().getName()).setLevel(Level.SEVERE);

            // 注册全局钩子
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException e) {
            e.printStackTrace();
            System.exit(1);
        }

        GlobalScreen.addNativeMouseMotionListener(new NativeMouseInputListener() {

            @Override
            public void nativeMouseMoved(NativeMouseEvent e) {
//                System.out.printf("[移动] X=%d, Y=%d\n", e.getX(), e.getY());
            }

            @Override
            public void nativeMouseDragged(NativeMouseEvent e) {
                //没用到
//                System.out.printf("[拖动] X=%d, Y=%d, Button=%d\n", e.getX(), e.getY(), e.getButton());
            }


            @Override
            public void nativeMousePressed(NativeMouseEvent e) {
                //这个不能用,用的下面的
                System.out.println("按下事件被触发【1】");
                System.out.printf("[按下] X=%d, Y=%d, Button=%d\n", e.getX(), e.getY(), e.getButton());
            }

            @Override
            public void nativeMouseClicked(NativeMouseEvent e) {
                //这个不能用,用的下面的
                System.out.println("点击事件被触发【1】");
                System.out.printf("[点击] X=%d, Y=%d, Button=%d\n", e.getX(), e.getY(), e.getButton());
            }

            @Override
            public void nativeMouseReleased(NativeMouseEvent e) {
                //这个不能用,用的下面的
                System.out.println("释放事件被触发【1】");
                System.out.printf("[释放] X=%d, Y=%d, Button=%d\n", e.getX(), e.getY(), e.getButton());
            }
        });

        GlobalScreen.addNativeMouseListener(new NativeMouseListener() {

            @Override
            public void nativeMouseClicked(NativeMouseEvent e) {
                System.out.println("点击事件被触发");
                System.out.printf("[点击] X=%d, Y=%d, Button=%d\n", e.getX(), e.getY(), e.getButton());
            }


            //nativeMousePressed 和 nativeMouseReleased 是配套使用的，它们共同描述了鼠标按键的完整交互过程-拖动
            @Override
            public void nativeMousePressed(NativeMouseEvent e) {
                System.out.println("按下事件被触发");
                //Button 1 代表左键,2代表右键
                System.out.printf("[按下] X=%d, Y=%d, Button=%d\n", e.getX(), e.getY(), e.getButton());
            }


            @Override
            public void nativeMouseReleased(NativeMouseEvent e) {
                System.out.println("释放事件被触发");
                System.out.printf("[释放] X=%d, Y=%d, Button=%d\n", e.getX(), e.getY(), e.getButton());
            }
        });


        // 添加滚轮监听器
        GlobalScreen.addNativeMouseWheelListener(new NativeMouseWheelListener() {
            @Override
            public void nativeMouseWheelMoved(NativeMouseWheelEvent e) {
                // 滚轮方向：wheelRotation > 0 表示向下滚，< 0 表示向上滚
                String direction = e.getWheelRotation() > 0 ? "向下" : "向上";
                System.out.printf("[滚轮] 方向=%s, 滚动量=%d, 类型=%s\n",
                        direction,
                        Math.abs(e.getWheelRotation()),
                        e.getScrollType() == NativeMouseWheelEvent.WHEEL_UNIT_SCROLL ? "单位滚动" : "块滚动"
                );
            }
        });

        // 关闭钩子时释放资源
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                GlobalScreen.unregisterNativeHook();
            } catch (NativeHookException e) {
                e.printStackTrace();
            }
        }));

        // 保持程序运行
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                break;
            }
        }
    }
}